<template>
  <div class="app-container define-table-content">
    <table-grid :reqData="reqData" :loading="reqData.loading" ref="tableGrid" :columnData="columnData">
    </table-grid>
  </div>
</template>

<script>
  import {
    query,
    createCode,
    createColumn
  } from '@/api/system/dbtemplate'
  import tableGrid from "@/views/master/Table";
  export default {
    name: 'sys-template',
    components: {
      tableGrid
    },
    data() {
      return {
        reqData: {
          loading: true,
          tableData: [],
          operation: {
            addCode: true,
            addColumn: true
          },
          // 合计条数
          total: 0,
          // 查询参数
          queryParams: {
            queryText: undefined,
            pageSize: 100,
            orderBy: 'Id',
            sort: 'descending'
          },
        },
        // 表格高度
        tableHeight: window.innerHeight,
        // 遮罩层
        // 返回结果集
        columnData: [{
          id: 1,
          name: "tableName",
          label: "表名",
          visibile: true
        }],
        // 选择表名列表
        selectTableNames: [],
        // 搜索条显示
        searchToggle: true
      }
    },
    created() {
      this.getList()
    },
    methods: {
      // 隐藏搜索
      handleSearch() {
        this.searchToggle = !this.searchToggle
      },
      /** 查询列表 */
      getList() {
        this.reqData.loading = true
        query(this.reqData.queryParams).then(res => {
          this.reqData.loading = false
          if (res.status) {
            this.reqData.tableData = res.outputData.DataSource
            this.reqData.total = res.outputData.TotalCount
          }
        })
      },
      // 删除按钮操作
      doAddCode() {
        this.selectTableNames = []
        var selectIds = this.$children[0].selectionIds
        console.log(JSON.stringify(selectIds))
        this.reqData.tableData.forEach(element => {
          if (selectIds.indexOf(element.Id) > -1) {
            this.selectTableNames.push(element.tableName)
          }
        })
        if (this.selectTableNames.length === 0) {
          return
        }
        this.$confirm(
            '确认创建 ' + this.selectTableNames.length + ' 条数据库表模板?',
            '警告', {
              confirmButtonText: '确定',
              cancelButtonText: '取消',
              type: 'warning'
            }
          )
          .then(() => {
            createCode({
              tableNames: JSON.stringify(this.selectTableNames)
            }).then(res => {
              if (res.status) {
                this.$message({
                  message: '操作成功',
                  type: 'success'
                })
                this.getList()
              }
            })
          })
          .catch(() => {})
      },
      doGenerateColumn() {
        this.selectTableNames = []
        var selectIds = this.$children[0].selectionIds
        console.log(JSON.stringify(selectIds))
        this.reqData.tableData.forEach(element => {
          if (selectIds.indexOf(element.Id) > -1) {
            this.selectTableNames.push(element.tableName)
          }
        })
        if (this.selectTableNames.length === 0) {
          return
        }
        this.$confirm(
            '确认创建 ' + this.selectTableNames.length + ' 条数据库表列字典?',
            '警告', {
              confirmButtonText: '确定',
              cancelButtonText: '取消',
              type: 'warning'
            }
          )
          .then(() => {
            createColumn({
              tableNames: JSON.stringify(this.selectTableNames)
            }).then(res => {
              if (res.status) {
                this.$message({
                  message: '操作成功',
                  type: 'success'
                })
                this.getList()
              }
            })
          })
          .catch(() => {})
      },
      doSearch(option) {
        if (option) {
          if (option.queryText) {
            this.reqData.queryParams.queryText = option.queryText;
          }
          if (option.queryData) {
            this.reqData.queryParams.queryData = JSON.stringify(option.queryData);
          }
          if (option.sortData) {
            this.reqData.queryParams.sortData = JSON.stringify(option.sortData);
          }
          if (option.focusId) {
            this.reqData.queryParams.focusId = option.focusId
          }
          if (option.groupData) {
            this.reqData.queryParams.groupData = JSON.stringify(option.groupData);
          }
          this.getList()
        }
      }
    }
  }
</script>
